package com.yic.module.device.service.inspectionRecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.device.controller.admin.inspectionRecord.vo.*;
import com.yic.module.device.dal.dataobject.inspectionRecord.InspectionRecordDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.device.convert.inspectionRecord.InspectionRecordConvert;
import com.yic.module.device.dal.mysql.inspectionRecord.InspectionRecordMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.device.enums.ErrorCodeConstants.*;

/**
 * 巡检/点检记录 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class InspectionRecordServiceImpl implements InspectionRecordService {

    @Resource
    private InspectionRecordMapper inspectionRecordMapper;

    @Override
    public Long createInspectionRecord(InspectionRecordCreateReqVO createReqVO) {
        // 插入
        InspectionRecordDO inspectionRecord = InspectionRecordConvert.INSTANCE.convert(createReqVO);
        inspectionRecordMapper.insert(inspectionRecord);
        // 返回
        return inspectionRecord.getId();
    }

    @Override
    public void updateInspectionRecord(InspectionRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateInspectionRecordExists(updateReqVO.getId());
        // 更新
        InspectionRecordDO updateObj = InspectionRecordConvert.INSTANCE.convert(updateReqVO);
        inspectionRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteInspectionRecord(Long id) {
        // 校验存在
        validateInspectionRecordExists(id);
        // 删除
        inspectionRecordMapper.deleteById(id);
    }

    private void validateInspectionRecordExists(Long id) {
        if (inspectionRecordMapper.selectById(id) == null) {
            throw exception(INSPECTION_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public InspectionRecordDO getInspectionRecord(Long id) {
        return inspectionRecordMapper.selectById(id);
    }

    @Override
    public List<InspectionRecordDO> getInspectionRecordList(Collection<Long> ids) {
        return inspectionRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InspectionRecordDO> getInspectionRecordPage(InspectionRecordPageReqVO pageReqVO) {
        return inspectionRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InspectionRecordDO> getInspectionRecordList(InspectionRecordExportReqVO exportReqVO) {
        return inspectionRecordMapper.selectList(exportReqVO);
    }

}
